package com.tc.dispatch.web.schedule.similary;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tc.dispatch.gsalgorithm.spec.parse;
import com.tc.dispatch.gsalgorithm.spec.spec;
import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.web.gsservice.gsHelper;

public class similary extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public similary() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		// response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		if (action == null)
			action = "";

		HttpSession sess = request.getSession();
		if(action.equals("doAction"))
		{//计算相似度
			try{
			String spec=request.getParameter("spec");
			String ssign=request.getParameter("ssign");
			String plate=request.getParameter("plate");
			
			String specComp=request.getParameter("specComp");
			String ssignComp=request.getParameter("ssignComp");
			String plateComp=request.getParameter("plateComp");
			spec spec1=parse.parseSpec(spec);
			spec spec2=parse.parseSpec(specComp);
			Float ret=com.tc.dispatch.gsservice.action.GSSimilary.calcSimilary(spec1, spec2);
			response.getWriter().print("{success:true,data:"+ret+"}");
			}
			catch(Exception e)
			{
				System.out.println("doAction error:"+e.getMessage());
				response.getWriter().print("{success:false,data:0}");
			}
		}
		out.flush();
		out.close();
	}

	public Float calcSim(String spec,String specComp)
	{

		spec spec1=parse.parseSpec(spec);
		spec spec2=parse.parseSpec(specComp);
		try{
		Float ret=com.tc.dispatch.gsservice.action.GSSimilary.calcSimilary(spec1, spec2);
		return ret;
		}
		catch(Exception e)
		{
			System.out.println("calcSim error:"+e.getMessage());
			return new Float(0);
		}
	}
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
